/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.menosehmais.framework.security;

import br.com.caelum.vraptor.*;
import br.com.menosehmais.controller.HomeController;
import br.com.menosehmais.controller.view.PosLoginController;
import br.com.menosehmais.framework.persistence.Id;
import br.com.menosehmais.framework.security.annotation.PermitAll;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Rafael Quintino
 */
@Resource
@PermitAll
public class LoginController {
  
    private Result result;
    private HttpServletRequest request;
    private UserSession userSession;
    private SecurityBO securityBO;
    
    public LoginController(Result result, HttpServletRequest request, UserSession userSession, SecurityBO securityBO){
        this.result = result;
        this.request = request;
        this.securityBO = securityBO;
    }
    
    @Get
    @Path("/ssl/loginForm")
    public void loginForm(){
        
    }
    
    @Get
    @Path("/ssl/createUser")
    public void createUser(){
        
    }
    
    @Post
    @Path("/ssl/askToResetPassword")
    public void askToResetPassword(String usuario){
        //...
    }
    
    @Get
    @Path("/ssl/resetPassword/{id}/{oldEncryptedPassword}")
    public void resetPassword(Id id, String oldEncryptedPassword){
        //...
    }

    @Post
    @Path("/ssl/login")
    //@Path("/rest/login")
    public void login(LoginDTO login){
        //validar login
        if(securityBO.login(request, userSession, login)) {
            result.redirectTo(PosLoginController.class).indexPosLogin();
        } else {
            result.include("error", true)
                  .include("login", login)
                  .redirectTo(LoginController.class).loginForm();
        }
    }

    @Path("/ssl/TESTE")
    public void teste() {
        System.out.println("TESTE");
        result.use(br.com.caelum.vraptor.view.Results.json()).from(new LoginDTO()).recursive().serialize();
    }


    @Get
    @Path("/ssl/logout")
    public void logout(){
        userSession.logout();
        result.redirectTo(HomeController.class).index();
    }

}
